<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"  %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript">
	function calcPrice() {
		var onceTicket = jQuery("#onceTicket");
		var twiceTicket = jQuery("#twiceTicket");
		var price = jQuery("#price");

		var onceTicketCount = onceTicket.val();
		var twiceTicketCount = twiceTicket.val();

		onceTicketCount = onceTicketCount *= 1;
		twiceTicketCount = twiceTicketCount *= 1;
		var totalPrice = 0;

		if (onceTicketCount + twiceTicketCount > 0) {
			var ticketPrice = '${ticketPrice}';
			ticketPrice = ticketPrice *= 1;

			var deliveryPrice = '${deliveryPrice}';
			deliveryPrice = deliveryPrice *= 1;

			totalPrice = (onceTicketCount * ticketPrice) + (twiceTicketCount * ticketPrice) + deliveryPrice;

			var reg = /(^[+-]?\d+)(\d{3})/;
			totalPrice += '';
			while (reg.test(totalPrice)) {
				totalPrice = totalPrice.replace(reg, '$1' + ',' + '$2');
			}
		}
		price.text(totalPrice);
	}

	function increaseOnceTicket() {
		var onceTicket = jQuery("#onceTicket");
		var value = onceTicket.val();
		value = value *= 1;

		var ticketMaxLimit = '${ticketMaxLimit}';
		ticketMaxLimit = ticketMaxLimit *= 1;

		if (value < ticketMaxLimit) {
			onceTicket.val(value + 1);
		} else {
			value = ticketMaxLimit;
		}
		calcPrice();
	}

	function decreaseOnceTicket() {
		var onceTicket = jQuery("#onceTicket");
		var value = onceTicket.val();
		value = value *= 1;
		if (value > 0) {
			onceTicket.val(value - 1);
		} else {
			value = 0;
		}
		calcPrice();
	}

	function increaseTwiceTicket() {
		var twiceTicket = jQuery("#twiceTicket");
		var value = twiceTicket.val();
		value = value *= 1;

		var ticketMaxLimit = '${ticketMaxLimit}';
		ticketMaxLimit = ticketMaxLimit *= 1;

		if (value < ticketMaxLimit) {
			twiceTicket.val(value + 1);
		} else {
			value = ticketMaxLimit;
		}
		calcPrice();
	}

	function decreaseTwiceTicket() {
		var twiceTicket = jQuery("#twiceTicket");
		var value = twiceTicket.val();
		value = value *= 1;
		if (value > 0) {
			twiceTicket.val(value - 1);
		} else {
			value = 0;
		}
		calcPrice();
	}

	function goPurchaseForm() {
		var onceTicket = jQuery("#onceTicket").val();
		var twiceTicket = jQuery("#twiceTicket").val();

		if (onceTicket + twiceTicket < 1) {
			alert('<spring:message code="error.ticket.nonselected"/>');
			jQuery("html, body").animate({ scrollTop: 0 }, 100);
			return false;
		}
		jQuery(location).attr("href", "/purchaseForm.do?onceTicket=" + onceTicket + "&twiceTicket=" + twiceTicket);
	}
</script>
</head>
</html>